<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController{
    public function index(){
        $model = M('Article'); 
        $rows = $model->select(); 
        $this->assign('rows', $rows);
        $this->display();

    }
    public function add(){
        if (IS_POST) {
            //过滤数据
            $data = I('post.');
            //设置自动验证规则 模型类文件中
            //设置自动完成规则 模型类文件中
            $model = D('Article');

            //自动创建数据
            $resArr = $model->create($data); 

            if($resArr === false || empty($resArr)){
                $errMsg = $model->getError();
                $this->error($errMsg ? $errMsg : '自动创建时未知错误');
            }
            // 补充数据
            if(isset($_POST['content'])){
                $pic = $model->get_imgsrc($_POST['content']);
                $pic = str_replace ( __ROOT__ , '', $pic);
                $model->pic = $pic; 
            }

            if($model->add()){
                $this->success('添加成功!');
            }else{
                $errMsg = $model->getError();
                $this->error($errMsg ? $errMsg : '添加时未知错误');
                //$this->error($model->getError());
            }
        }else{
             //$this->title = '添加文章';
            $data = M('Category')->field('id,title')->select();
            $this->assign('plists',$data);

            $this->display('edit');
        }
    }
    public function edit(){
        if (IS_POST) {
            //过滤数据
            $data = I('post.');
            $model = D('Article'); 
            //设置自动验证规则 模型类文件中
            //设置自动完成规则 模型类文件中

            //自动创建数据
            $resArr = $model->create($data);
            if($resArr === false || empty($resArr)){
                $errMsg = $model->getError();
                $this->error($errMsg ? $errMsg : '未知错误');
            }

            //完善 必须创建完成之后才可以补充 
            if(isset($_POST['content'])){
                $pic = $model->get_imgsrc($_POST['content']);
                $pic = str_replace ( __ROOT__ , '', $pic);// __ROOT__ =  /robot_henan
                //<img  src="__ROOT__$pic"  />'
                $model->pic = $pic; 
            }

            if($model->save()){
                $this->success('更新成功');
            }else{
                $this->error($model->getError());
            }
        }else{
            $id = I('get.id');
            $model = M('Article');
            $row = $model->find($id);
            $row['content'] = htmlspecialchars_decode($row['content']);//内容反处理
            $this->assign('row', $row);

            //assign category
            $data = M('Category')->field('id,title')->select();
            $this->assign('plists',$data);

            $this->display('edit');
            
        }
    }
    public function delDo(){
        $id = I('get.id');
        $model = M('Article');

        if ($model->delete($id)) {
            $this->success('删除成功');
        }else{
            $this->error($model->getError());
        }
    }
}
